package com.xadmin.designing.controller;

import com.xadmin.sys.common.response.Result;
import com.xadmin.sys.common.utils.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;



import com.xadmin.designing.pojo.Test02;
import com.xadmin.designing.service.Test02Service;

@RestController
@RequestMapping("/test02")
@Api(value = "t2模块",tags = "t2模块")
public class Test02Controller {

    @Resource
    private Test02Service serviceTmp;

    @PreAuthorize("hasAnyRole('SUPERADMIN','COMMON')")
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    @ApiOperation(value = "分页查询",notes = "分页查询")
    public Result getByPage(@RequestBody Page<Test02> page){
        page = serviceTmp.getAllByPage(page);
        return  Result.ok().data("data",page);
    }

    @PreAuthorize("hasAnyRole('SUPERADMIN','COMMON')")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加",notes = "添加")
    public Result add(@RequestBody Test02 dataList){
        boolean  check =serviceTmp.save(dataList);
        if(check){
            return  Result.ok().message("添加成功！");
        }else {
            return Result.error().message("添加失败！");
        }
    }

    @PreAuthorize("hasAnyRole('SUPERADMIN','COMMON')")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除",notes = "删除")
    public Result delete(@PathVariable Integer id){
        boolean check = serviceTmp.removeById(id);
        if (check == true){
        return  Result.ok().message("删除成功！");
        }else{
        return Result.error().message("删除失败！");
        }
    }

    @PreAuthorize("hasAnyRole('SUPERADMIN','COMMON')")
    @RequestMapping(value = "/view/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "根据ID查看",notes = "根据ID查看")
    public Result view(@PathVariable Integer id){
        Test02 dataList = serviceTmp.getById(id);
        return  Result.ok().data("data",dataList);
    }

    @PreAuthorize("hasAnyRole('SUPERADMIN','COMMON')")
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ApiOperation(value = "根据ID更新",notes = "根据ID更新")
    public Result update(@RequestBody Test02 dataList){
        boolean check = serviceTmp.updateById(dataList);
        if (check == true){
            return  Result.ok().message("更新成功！");
        }else{
            return Result.error().message("更新失败！");
        }
    }
}

